
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;

public class AlphaGUI extends JFrame {
    private static final long serialVersionUID = -4560890492830654624L;

    private JSpinner tempoSpinner;
    private JSpinner beatsSpinner;
    private JSpinner barsSpinner;

    public static void main(String[] args) {
        AlphaGUI window = new AlphaGUI(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.out.println("START!");
            }
        }, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.out.println("SAVE!");
            }
        }, new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
                System.out.println("metronome!");
           } 
        },   new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent event) {
                   System.out.println("PAUSE!");
              }
        });
        window.setVisible(true);
    }

    //sss
    public AlphaGUI(final ActionListener startListener, final ActionListener keepListener, 
        final ActionListener metronomeListener, final ActionListener pauseListener) {
        // Try to make the GUI look like native GUI:
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable t) {
            // Don't worry if that goes wrong.
        }

        Container content = this.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        // Timings section:
        Container timings = new JPanel();
        timings.setLayout(new GridLayout(3, 2));

        tempoSpinner = new JSpinner(new SpinnerNumberModel(120, 1, 400, 1));
        beatsSpinner = new JSpinner(new SpinnerNumberModel(4, 1, 32, 1));
        barsSpinner = new JSpinner(new SpinnerNumberModel(4, 1, 32, 1));

        timings.add(new Label("Tempo"));
        timings.add(tempoSpinner);

        timings.add(new Label("Beats per bar"));
        timings.add(beatsSpinner);

        timings.add(new Label("Bars per loop"));
        timings.add(barsSpinner);
        //fgj

        // Controls section:
        Container controls = new JPanel();
        controls.setLayout(new BoxLayout(controls, BoxLayout.X_AXIS));

        final Button start = new Button("Start");
        start.addActionListener(startListener);
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                start.setEnabled(false);
            }
        });
        controls.add(start);

        final Button keep = new Button("Keep");
        keep.addActionListener(keepListener);
        controls.add(keep);
        
        final Button metronome = new Button("Metronome");
        metronome.addActionListener(metronomeListener);
        controls.add(metronome);
        
        final Button pause = new Button("Pause");
        pause.addActionListener(pauseListener);
        controls.add(pause);
        //

        content.add(timings);
        content.add(controls);

        content.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent event) {
                if (event.getKeyCode() == KeyEvent.VK_SPACE) {
                    keepListener.actionPerformed(new ActionEvent(keep, ActionEvent.ACTION_PERFORMED, keep.getLabel()));
                }
            }
        });

        this.setTitle("Figure8");
        this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        this.pack();
    }

    public int getTempo() {
        return (Integer) tempoSpinner.getValue();
    }

    public int getBeats() {
        return (Integer) beatsSpinner.getValue();
    }

    public int getBars() {
        return (Integer) barsSpinner.getValue();
    }
}
